package com.wsjj.yjh;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class TableStreamDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        DataStreamSource<WaterSensor> sensorDS = env.fromElements(
                new WaterSensor("s1", 1L, 1),
                new WaterSensor("s1", 2L, 2),
                new WaterSensor("s2", 2L, 2),
                new WaterSensor("s3", 3L, 3),
                new WaterSensor("s3", 4L, 4)
        );

        Table table = tableEnv.fromDataStream(sensorDS);
        tableEnv.createTemporaryView("sensor",table);
        Table filterTable = tableEnv.sqlQuery("select id,ts,vc from sensor where ts > 2 ");
        Table sumTable = tableEnv.sqlQuery("select id,sum(vc) from sensor group by id");

        tableEnv.toDataStream(filterTable, WaterSensor.class).print("filter");

        tableEnv.toDataStream(sumTable).print("sum");



        env.execute();




    }
}
